package com.hj.deliveryorder.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.hj.common.core.domain.entity.SysUser;
import com.hj.deliveryorder.domain.DeliveryOrders;
import com.hj.deliveryorder.service.IDeliveryOrdersService;
import com.hj.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hj.common.annotation.Log;
import com.hj.common.core.controller.BaseController;
import com.hj.common.core.domain.AjaxResult;
import com.hj.common.enums.BusinessType;
import com.hj.common.utils.poi.ExcelUtil;
import com.hj.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 配送手机卡号的订单Controller
 * 
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/system/orders")
public class DeliveryOrdersController extends BaseController
{
    @Autowired
    private IDeliveryOrdersService deliveryOrdersService;
    @Autowired
    private ISysUserService userService;


    /**
     * 查询配送手机卡号的订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeliveryOrders deliveryOrders)
    {
        startPage();
        List<DeliveryOrders> list = deliveryOrdersService.selectDeliveryOrdersList(deliveryOrders);
        return getDataTable(list);
    }

    /**
     * 查询配送手机卡号的订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:list')")
    @GetMapping("/orderCount")
    public  AjaxResult orderCount(DeliveryOrders deliveryOrders)
    {
        SysUser sysUser = userService.selectUserById(deliveryOrders.getUserId());

        deliveryOrders.setDeptId(sysUser.getDeptId());
        deliveryOrders.setUserId(sysUser.getUserId());
        Integer count = deliveryOrdersService.orderCount(deliveryOrders);
        return success(count);
    }

    @Log(title = "工单管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:orders:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<DeliveryOrders> util = new ExcelUtil<DeliveryOrders>(DeliveryOrders.class);
        List<DeliveryOrders> orderList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = deliveryOrdersService.importOrder(orderList, updateSupport, operName);
        return success(message);
    }

    /**
     * 导出配送手机卡号的订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orders:export')")
    @Log(title = "配送手机卡号的订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeliveryOrders deliveryOrders)
    {
        List<DeliveryOrders> list = deliveryOrdersService.selectDeliveryOrdersList(deliveryOrders);
        ExcelUtil<DeliveryOrders> util = new ExcelUtil<DeliveryOrders>(DeliveryOrders.class);
        util.exportExcel(response, list, "配送手机卡号的订单数据");
    }

    /**
     * 获取配送手机卡号的订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deliveryOrdersService.selectDeliveryOrdersById(id));
    }

    /**
     * 新增配送手机卡号的订单
     */
    @PreAuthorize("@ss.hasPermi('system:orders:add')")
    @Log(title = "配送手机卡号的订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeliveryOrders deliveryOrders)
    {
        return toAjax(deliveryOrdersService.insertDeliveryOrders(deliveryOrders));
    }

    /**
     * 修改配送手机卡号的订单
     */
    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
    @Log(title = "配送手机卡号的订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeliveryOrders deliveryOrders)
    {
        return toAjax(deliveryOrdersService.updateDeliveryOrders(deliveryOrders));
    }

    /**
     * 删除配送手机卡号的订单
     */
    @PreAuthorize("@ss.hasPermi('system:orders:remove')")
    @Log(title = "配送手机卡号的订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deliveryOrdersService.deleteDeliveryOrdersByIds(ids));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<DeliveryOrders> util = new ExcelUtil<DeliveryOrders>(DeliveryOrders.class);
        util.importTemplateExcel(response, "订单数据");
    }


    /**
     * 分配订单
     */
    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
    @Log(title = "分配订单", businessType = BusinessType.UPDATE)
    @PostMapping("/assignOrders")
    public AjaxResult assignOrders(@RequestBody DeliveryOrders deliveryOrders)
    {
        SysUser user = getLoginUser().getUser();
        user.setUserId(deliveryOrders.getUserId());
        user.setUserName(   deliveryOrders.getUserName());
        return toAjax(deliveryOrdersService.assignOrders(deliveryOrders,user));
    }
}
